import { defineConfig } from 'rollup';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import babel from '@rollup/plugin-babel';
import dts from 'rollup-plugin-dts';
import clear from 'rollup-plugin-clear';
import fileSize from 'rollup-plugin-filesize';
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';

export default defineConfig([
	{
		input: './src/task-queue.ts',
		output: [
			{
				file: './dist/index.js',
				format: 'esm',
				sourcemap: true,
			},

			// 挂载window上的全局变量
			{
				file: './dist/iife/index.js',
				format: 'iife',
				name: 'Storage',
				sourcemap: true,
			},
		],
		plugins: [
			// 清空dist目录
			clear({
				targets: ['./dist'],
			}),

			// 别名
			alias({
				entries: [
					{
						find: '@',
						replacement: new URL('./src', import.meta.url).pathname,
					},
				],
			}),
			replace({
				preventAssignment: true,
				'process.env.NODE_ENV': JSON.stringify('development'),
			}),
			nodeResolve(),
			commonjs(),
			json(),
			typescript(),
			babel({
				// runtimeHelpers: true,
				exclude: 'node_modules/**',
				// externalHelpers: true,
				babelHelpers: 'bundled',
				extensions: ['.js'],
			}),

			// 显示打包体积
			fileSize(),

			// 热更新 默认监听根文件夹
			livereload(),

			// 本地服务器
			serve({
				open: false, // 自动打开页面
				port: 19899,
				openPage: '/public/index.html', // 打开的页面
				contentBase: '',
			}),
		],
	},
	{
		input: './src/task-queue.ts',
		output: [{ file: 'dist/index.d.ts', format: 'es' }],
		plugins: [dts()],
	},
]);
